package com.luoxiang.socialandroid.fragment;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.activity.UserZhifupayActivity;
import com.luoxiang.socialandroid.adapter.JifentixianAdapter;
import com.luoxiang.socialandroid.base.LazyLoadFragment;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.bean.SeeZhifupayBean;
import com.luoxiang.socialandroid.bean.TixianliebiaoBean;
import com.luoxiang.socialandroid.utils.AESUtil;
import com.luoxiang.socialandroid.utils.MmkvUtil;
import com.luoxiang.socialandroid.utils.NetUtils;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @Description TODO
 * @systemUser Administrator
 * @Author LX
 * @Date 09-28-2022 周三 14:58
 */
public class JifentixianFragment extends LazyLoadFragment {
    private String token;
    private TextView tv_alipayid;
    private RecyclerView rv_jifentixian;
    private JifentixianAdapter adapter;
    private Button btn_buycoin;
    private int taocanid;
    private RelativeLayout rele_isgozhifubao;

    @Override
    protected int setContentView() {
        return R.layout.fragment_jifentixian;
    }

    @Override
    protected void initView() {
        token = MmkvUtil.INSTANCE.decodeString(TOKEN);
        tv_alipayid = findViewById(R.id.tv_alipayid);
        rv_jifentixian = findViewById(R.id.rv_jifentixian);
        btn_buycoin = findViewById(R.id.btn_buycoin);
        rele_isgozhifubao = findViewById(R.id.rele_isgozhifubao);

    }

    private void initListener() {
        btn_buycoin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击提现按钮
                Integer positionid = MmkvUtil.INSTANCE.decodeInt("positionid");
                if (positionid != null) {
                    withdrawPackage(EXTRA, token, positionid, 1);
                } else {
                    withdrawPackage(EXTRA, token, taocanid, 1);
                }
            }
        });
    }

    @Override
    protected void lazyLoad() {
        getWithdrawPackages(EXTRA, token);
        initListener();
    }

    @Override
    public void onResume() {
        super.onResume();
        getWithdrawPackages(EXTRA, token);
    }

    /**
     * 积分提现列表接口
     *
     * @param extra
     * @param token
     */
    private void getWithdrawPackages(String extra, String token) {
        getRequest();
        NetUtils.getInstance().getApiService().getWithdrawPackages(extra, token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        clearLoading();
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        com.blankj.utilcode.util.LogUtils.i("decryptStr189", decryptStr);
                        Gson gson = new Gson();
                        TixianliebiaoBean tixianliebiaoBean = gson.fromJson(decryptStr, TixianliebiaoBean.class);
                        if (tixianliebiaoBean != null) {
                            if (tixianliebiaoBean.getData() != null) {
                                if (tixianliebiaoBean.getData().getAliInfos() != null) {
                                    String aliPayNo = tixianliebiaoBean.getData().getAliInfos().getAliPayNo();
                                    if (aliPayNo != null) {
                                        tv_alipayid.setText(aliPayNo);
                                        rele_isgozhifubao.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                Intent intent = new Intent(getContext(), UserZhifupayActivity.class);
                                                startActivity(intent);
                                            }
                                        });
                                    } else {
                                        tv_alipayid.setText(R.string.mi_weibangding);
                                        rele_isgozhifubao.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                Intent intent = new Intent(getContext(), UserZhifupayActivity.class);
                                                startActivity(intent);
                                            }
                                        });
                                    }
                                }
                                taocanid = tixianliebiaoBean.getData().getWithdraws().get(0).getId();
                            }
                            adapter = new JifentixianAdapter(tixianliebiaoBean.getData().getWithdraws());
                            GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
                            rv_jifentixian.setLayoutManager(gridLayoutManager);
                            rv_jifentixian.setAdapter(adapter);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        clearLoading();
                    }

                    @Override
                    public void onComplete() {
                        clearLoading();
                    }
                });
    }

    /**
     * 支付宝提现
     *
     * @param extra
     * @param token
     * @param packageId
     * @param way
     */
    private void withdrawPackage(String extra, String token, int packageId, int way) {
        NetUtils.getInstance().getApiService().withdrawPackage(extra, token, packageId, way)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        com.blankj.utilcode.util.LogUtils.i("decryptStr186", decryptStr);
                        Gson gson = new Gson();
                        SeeZhifupayBean seeZhifupayBean = gson.fromJson(decryptStr, SeeZhifupayBean.class);
                        if (seeZhifupayBean != null) {
                            if (seeZhifupayBean.getCode() == 0) {
                                LiveEventBus.get("tixianshuaxin").post(null);
                                ToastUtils.showShort(R.string.mi_tixianyitigaoshenhe);
                            } else {
                                ToastUtils.showShort(seeZhifupayBean.getMsg());
                            }
                            if (seeZhifupayBean.getMsg().equals(getString(R.string.mi_haiweibangdingzhifubao))) {
                                Intent intent = new Intent(getContext(), UserZhifupayActivity.class);
                                startActivity(intent);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
